const http = require('http')
const url = require('url')
const fs = require('fs')
const server = http.createServer((req, res) => {
  console.log(req.url)
  console.log(url.parse(req.url))
  if (req.url === '/upload') {
    // 直接这样图片上传是不行的,因为上传的图片其实是有其他的信息
    const fileWriter = fs.createWriteStream('./11.png', { flags: 'a+' })
    // 以下两种写法都可以
    // req.pipe(fileWriter)
    req.on('data', (chunk) => {
      fileWriter.write(chunk)
    })
    req.on('end', () => {
      console.log('文件上传成功')
    })
  }
  res.end('文件上传成功')
})

const port = 8000
server.listen(port, () => {
  console.log(`${port}服务开启`)
})
